<template>
  <view class="coupon-center">
    <!-- 顶部导航栏 -->
    <!-- <view class="nav-bar">
      <view class="nav-left" @click="goBack">
        <uni-icons type="left" size="20" color="#333"></uni-icons>
      </view>
      <view class="nav-title">领券中心</view>
      <view class="nav-right">
        <uni-icons type="more-filled" size="20" color="#666"></uni-icons>
        <uni-icons type="minus" size="20" color="#666"></uni-icons>
        <uni-icons type="circle" size="20" color="#666"></uni-icons>
      </view>
    </view> -->

    <!-- 优惠券列表 -->
    <view class="coupon-list">
      <view 
        v-for="(coupon, index) in couponList" 
        :key="index"
        class="coupon-item"
        @click="claimCoupon(coupon)"
      >
        <!-- 左侧优惠券信息 -->
        <view class="coupon-left">
          <view class="discount-info">
            <text class="discount-value">{{ coupon.discountValue }}</text>
            <text class="discount-unit">{{ coupon.discountUnit }}</text>
          </view>
          <view class="usage-condition">
            满{{ coupon.minAmount }}元可用
          </view>
        </view>

        <!-- 右侧详细信息 -->
        <view class="coupon-right">
          <view class="coupon-details">
            <view class="shop-name">{{ coupon.shopName }}</view>
            <view class="valid-date">有效期至: {{ coupon.validDate }}</view>
          </view>
          
          <!-- 券字水印 -->
          <view class="coupon-watermark">券</view>
          
          <!-- 领取按钮 -->
          <view class="claim-btn" :class="{ claimed: coupon.isClaimed }">
            {{ coupon.isClaimed ? '已领取' : '点击领取' }}
          </view>
        </view>

        <!-- 优惠券边缘装饰 -->
        <view class="coupon-edge">
          <view class="edge-circle" v-for="i in 6" :key="i"></view>
        </view>
      </view>
    </view>

    <!-- 空状态 -->
    <view class="empty-state" v-if="couponList.length === 0">
      <image class="empty-img" src="/static/img/a.png" mode="aspectFit"></image>
      <text class="empty-text">暂无优惠券</text>
    </view>

    <!-- 加载更多 -->
    <view class="load-more" v-if="hasMore && couponList.length > 0">
      <text class="load-text" @click="loadMore">加载更多</text>
    </view>
  </view>
</template>

<script>
	import {
			get,
			post
		} from '@/config/request'
	
export default {
  data() {
    return {
      page: 1, // 当前页码
      pageSize: 10, // 每页数量
      hasMore: true, // 是否还有更多数据
      loading: false, // 是否正在加载
      
      // 优惠券列表 - 后续对接接口
      couponList: [
		  
	  ]
    }
  },
  
  onLoad() {
    // 页面加载时获取优惠券数据
    this.getCouponList(),
	this.youhuijuan()
  },
  
  // 下拉刷新
  onPullDownRefresh() {
    this.refreshCouponList()
  },
  
  // 上拉加载更多
  onReachBottom() {
    if (this.hasMore && !this.loading) {
      this.loadMore()
    }
  },

  methods: {
	 //  async youhuijuan(){
	 //  	const res =await get('/promotion1/lingJuanZhongXin/list',{})
	 //  	console.log(res)
		// this.couponList = res.rows
	 //  },
    // 返回上一页
    goBack() {
      uni.navigateBack()
    },
    
    // 获取优惠券列表 - 后续对接接口
    getCouponList() {
      this.loading = true
      console.log('获取优惠券列表')
      this.loading = false
    },
    
    // 刷新优惠券列表
    refreshCouponList() {
      this.page = 1
      this.hasMore = true
      this.getCouponList()
      uni.stopPullDownRefresh()
    },
    
    // 加载更多
    loadMore() {
      if (this.loading || !this.hasMore) return
      
      this.page++
      this.getCouponList()
    },
    
    // 领取优惠券
    claimCoupon(coupon) {
      if (coupon.isClaimed) {
        uni.showToast({
          title: '已领取过此优惠券',
          icon: 'none'
        })
        return
      }
      
      // TODO: 调用领取优惠券接口
      // uni.request({
      //   url: '/api/coupons/claim',
      //   method: 'POST',
      //   data: {
      //     couponId: coupon.id
      //   },
      //   success: (res) => {
      //     if (res.data.success) {
      //       // 更新本地状态
      //       coupon.isClaimed = true
      //       uni.showToast({
      //         title: '领取成功',
      //         icon: 'success'
      //       })
      //     } else {
      //       uni.showToast({
      //         title: res.data.message || '领取失败',
      //         icon: 'none'
      //       })
      //     }
      //   },
      //   fail: () => {
      //     uni.showToast({
      //       title: '网络错误，请重试',
      //       icon: 'none'
      //     })
      //   }
      // })
      
      // 模拟领取成功
      coupon.isClaimed = true
      uni.showToast({
        title: '领取成功',
        icon: 'success'
      })
      
      console.log('领取优惠券:', coupon.id)
    }
  }
}
</script>

<style scoped>
.coupon-center {
  background-color: #f8f9fa;
  min-height: 100vh;
  padding: 20rpx;
  padding-bottom: env(safe-area-inset-bottom);
}

/* 优惠券列表 */
.coupon-list {
  display: flex;
  flex-direction: column;
  gap: 20rpx;
}

.coupon-item {
  position: relative;
  background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 100%);
  border-radius: 16rpx;
  padding: 30rpx;
  display: flex;
  align-items: center;
  box-shadow: 0 4rpx 16rpx rgba(255, 107, 107, 0.3);
  transition: all 0.2s ease;
  overflow: hidden;
}

.coupon-item:active {
  transform: scale(0.98);
  box-shadow: 0 2rpx 8rpx rgba(255, 107, 107, 0.4);
}

/* 左侧优惠券信息 */
.coupon-left {
  flex-shrink: 0;
  text-align: center;
  color: #fff;
  margin-right: 30rpx;
}

.discount-info {
  display: flex;
  align-items: baseline;
  justify-content: center;
  margin-bottom: 8rpx;
}

.discount-value {
  font-size: 48rpx;
  font-weight: bold;
  line-height: 1;
}

.discount-unit {
  font-size: 24rpx;
  font-weight: 500;
  margin-left: 4rpx;
}

.usage-condition {
  font-size: 24rpx;
  opacity: 0.9;
  line-height: 1.2;
}

/* 右侧详细信息 */
.coupon-right {
  flex: 1;
  position: relative;
  color: #fff;
}

.coupon-details {
  margin-bottom: 20rpx;
}

.shop-name {
  font-size: 32rpx;
  font-weight: 600;
  margin-bottom: 8rpx;
  line-height: 1.2;
}

.valid-date {
  font-size: 24rpx;
  opacity: 0.8;
  line-height: 1.2;
}

/* 券字水印 */
.coupon-watermark {
  position: absolute;
  top: 50%;
  right: 20rpx;
  transform: translateY(-50%);
  font-size: 120rpx;
  color: rgba(255, 255, 255, 0.1);
  font-weight: bold;
  line-height: 1;
}

/* 领取按钮 */
.claim-btn {
  position: absolute;
  bottom: 0;
  right: 0;
  background-color: rgba(255, 255, 255, 0.2);
  color: #fff;
  font-size: 24rpx;
  padding: 8rpx 16rpx;
  border-radius: 12rpx;
  border: 1rpx solid rgba(255, 255, 255, 0.3);
  transition: all 0.2s ease;
}

.claim-btn.claimed {
  background-color: rgba(255, 255, 255, 0.1);
  color: rgba(255, 255, 255, 0.6);
}

.claim-btn:active {
  background-color: rgba(255, 255, 255, 0.3);
  transform: scale(0.95);
}

/* 优惠券边缘装饰 */
.coupon-edge {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 20rpx;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
}

.edge-circle {
  width: 12rpx;
  height: 12rpx;
  background-color: #f8f9fa;
  border-radius: 50%;
}

/* 空状态 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 100rpx 0;
  color: #999;
}

.empty-img {
  width: 200rpx;
  height: 200rpx;
  margin-bottom: 30rpx;
  opacity: 0.5;
}

.empty-text {
  font-size: 28rpx;
  color: #999;
}

/* 加载更多 */
.load-more {
  text-align: center;
  padding: 30rpx 0;
}

.load-text {
  font-size: 28rpx;
  color: #666;
  padding: 16rpx 32rpx;
  background-color: #fff;
  border-radius: 20rpx;
  border: 1rpx solid #e8e8e8;
  transition: all 0.2s ease;
}

.load-text:active {
  background-color: #f8f9fa;
  transform: scale(0.95);
}

/* 响应式适配 */
@media screen and (max-width: 750rpx) {
  .coupon-center {
    padding: 15rpx;
  }
  
  .coupon-item {
    padding: 25rpx;
  }
  
  .discount-value {
    font-size: 40rpx;
  }
  
  .shop-name {
    font-size: 28rpx;
  }
  
  .coupon-watermark {
    font-size: 100rpx;
  }
}

@media screen and (max-width: 600rpx) {
  .coupon-center {
    padding: 10rpx;
  }
  
  .coupon-item {
    padding: 20rpx;
  }
  
  .discount-value {
    font-size: 36rpx;
  }
  
  .shop-name {
    font-size: 26rpx;
  }
  
  .coupon-watermark {
    font-size: 80rpx;
  }
  
  .claim-btn {
    font-size: 22rpx;
    padding: 6rpx 12rpx;
  }
}
</style>
